//
//  ZYLFastLookBaseViewController.m
//  Cartoon
//
//  Created by dllo on 15/7/13.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)

#import "ZYLFastLookBaseViewController.h"
#import "ZYLFastLookTableViewCell.h"
#import "ZYLFastLookModel.h"
#import "AFNetworkHandler.h"
#import "ZYLSearchBaseViewController.h"
#import "ReadViewController.h"
#import "ZYLAuthorBaseViewController.h"
#import "ZYLFastlookAllContentViewController.h"

@interface ZYLFastLookBaseViewController () <UITableViewDataSource, UITableViewDelegate, PushToOtherViewController>

@property (nonatomic, retain) NSMutableArray *tagArr;
@property (nonatomic, retain) NSMutableArray *colors;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIView *stick;
@property (nonatomic, retain) UIView *headerTitle;
@property (nonatomic, retain) UILabel *titleName;
@property (nonatomic, retain) UIButton *searchButton;
@property (nonatomic, retain) NSMutableArray *searchIcon;
@property (nonatomic, retain) NSMutableArray *emojiText;


@end

@implementation ZYLFastLookBaseViewController

- (void)dealloc
{
    [_tagArr release];
    [_colors release];
    [_tableView release];
    [_headerTitle release];
    [_stick release];
    [_emojiText release];
    [_searchIcon release];
    [_searchButton release];
    [super dealloc];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    UIColor *temp = self.stick.backgroundColor;
    NSString *tempName = self.titleName.text;
    
    for (; 1; ) {
        NSInteger i = arc4random() % 3;
        NSInteger e = arc4random() % self.emojiText.count;
        
        self.stick.backgroundColor = self.colors[i];
        self.titleName.textColor = self.stick.backgroundColor;
        self.titleName.text = [NSString stringWithFormat:@"今日推荐  %@", self.emojiText[e]];
        [self.searchButton setImage:[UIImage imageNamed:self.searchIcon[i]] forState:UIControlStateNormal];
        if (temp != self.stick.backgroundColor && ![tempName isEqualToString:self.titleName.text]) {
            
            break;
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
    self.colors = [NSMutableArray arrayWithObjects:[UIColor colorWithRed:0.9333 green:0.4471 blue:0.4392 alpha:1], [UIColor colorWithRed:0.2 green:0.6549 blue:0.9137 alpha:1], [UIColor colorWithRed:1.0 green:0.698 blue:0.4196 alpha:1], nil];
    self.searchIcon = [NSMutableArray arrayWithObjects:@"iconfont-searchvc-0.png", @"iconfont-searchvc-1.png", @"iconfont-searchvc-2.png", nil];
    self.emojiText = [NSMutableArray arrayWithObjects:@"(´ﾟДﾟ`)", @"(=ﾟ∀ﾟ)=", @"(|||ﾟДﾟ)", @"Σ( ﾟдﾟ)", @"(ノﾟ∀ﾟ)ノ", @"(￣∇￣)", @"(ﾟДﾟ≡ﾟДﾟ)", @"(;´Д`)",@"(つд⊂)", @"(＾o＾)ﾉ", @"|ー`)", @" ﾟ∀ﾟ)σﾟ ∀ﾟ)ノ", @"( ´_ゝ`)", nil];
    [self createView];
    
}


- (void)searchAction
{
    ZYLSearchBaseViewController *searchVC = [[[ZYLSearchBaseViewController alloc] init] autorelease];
    UINavigationController *navi = [[[UINavigationController alloc] initWithRootViewController:searchVC] autorelease];
    [self presentViewController:navi animated:YES completion:^{
        
    }];
}

- (void)inferData:(NSDictionary *)result;
{
    //self.tagArr = [NSMutableArray array];
    NSMutableArray *tempArr = [[result objectForKey:@"data"] objectForKey:@"comics"];
    self.tagArr = [ZYLFastLookModel arrayWithModelByArray:tempArr];
    
}

- (void)createView
{
    self.headerTitle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    self.stick = [[UIView alloc] initWithFrame:CGRectMake(13, 26.5, 5, 22)];
    self.headerTitle = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)] autorelease];
    self.stick = [[[UIView alloc] initWithFrame:CGRectMake(13, 26.5, 5, 22)] autorelease];
    self.headerTitle.backgroundColor = [UIColor colorWithRed:0.975 green:0.975 blue:0.975 alpha:1];
    self.stick.backgroundColor = self.colors[0];
    self.stick.layer.cornerRadius = 2.5;
    UIView *stickShadow = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 22)];
    UIView *stickShadow = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 22)] autorelease];
    stickShadow.layer.cornerRadius = 2.5;
    stickShadow.layer.borderWidth = 1.5;
    stickShadow.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor;
    [self.stick addSubview:stickShadow];
    
    [self.headerTitle addSubview:self.stick];
    [self.view addSubview:self.headerTitle];
    [_headerTitle release];
    [_stick release];
    self.titleName = [[UILabel alloc] initWithFrame:CGRectMake(25, 25, 250, 25)];
    self.titleName = [[[UILabel alloc] initWithFrame:CGRectMake(25, 25, 250, 25)] autorelease];
    self.titleName.text = @"今日推荐";
    self.titleName.font = [UIFont systemFontOfSize:18];
    [self.headerTitle addSubview:self.titleName];
    [_titleName release];
    self.searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.searchButton.frame = CGRectMake(SCREEN_WIDTH - 35, 25, 25, 25);
    [self.searchButton addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
    [self.headerTitle addSubview:self.searchButton];
    
    
    
    
    UIView *shadowView = [[UIView alloc] initWithFrame:CGRectMake(0, 63.5, SCREEN_WIDTH, 0.5)];
    UIView *shadowView = [[[UIView alloc] initWithFrame:CGRectMake(0, 63.5, SCREEN_WIDTH, 0.5)] autorelease];
    shadowView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    [self.headerTitle addSubview:shadowView];
    [shadowView release];
    [self.view bringSubviewToFront:shadowView];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 108) style:UITableViewStylePlain];
    self.tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 108) style:UITableViewStylePlain] autorelease];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.bounces = NO;
    [AFNetworkHandler GETwithAFNByURL:@"http://api.kuaikanmanhua.com/v1/comic_lists/1?offset=0&limit=50" completion:^(id result) {
        [self inferData:(NSDictionary *)result];
        [self.view addSubview:self.tableView];
        [_tableView release];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tagArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ZYLFastLookTableViewCell *cell = [tableView
        dequeueReusableCellWithIdentifier:nil];
    if (!cell) {
            cell = [[[ZYLFastLookTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        }
    cell.pushToOtherDelegate = self;
    if (indexPath.row  % 3 == 0) {
            cell.praise.backgroundColor = self.colors[0];
            cell.comment.backgroundColor =self.colors[0];
            cell.share.backgroundColor = self.colors[0];
    } else if (indexPath.row % 3 == 1) {
            cell.praise.backgroundColor = self.colors[1];
            cell.comment.backgroundColor = self.colors[1];
            cell.share.backgroundColor = self.colors[1];
    } else {
            cell.praise.backgroundColor = self.colors[2];
            cell.comment.backgroundColor = self.colors[2];
            cell.share.backgroundColor = self.colors[2];
    }
        //cell.cover.image = [UIImage imageNamed:@"suibian 1.jpg"];
//        cell.title.text = @"#日常";
//        cell.partInfo.text = @"#第二季 第二集";
//        cell.authorImage.image = [UIImage imageNamed:@"iconfont-user.png"];
//        cell.author.text = @"koyoto Animation";
//        cell.shareCount.text = @"984";
//        cell.commentCount.text = @"123";
//        cell.praiseCount.text = @"3424";
    cell.model = self.tagArr[indexPath.row];
    
        return cell;
       
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 94 + 0.48 * SCREEN_WIDTH;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//{
//    return 64.0;
//}



//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)] autorelease];
//    headerView.backgroundColor = [UIColor whiteColor];
//    return headerView;
//    
//}

- (void)pushToOtherViewControllerWithZYLFastLookModel:(ZYLFastLookModel *)model keyword:(NSString *)keyword
{
    if ([keyword isEqualToString:@"图片"]) {
        ReadViewController *readVC = [[[ReadViewController alloc] init] autorelease];
        readVC.ID = model.ID;
        readVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:readVC animated:YES];
    } else if ([keyword isEqualToString:@"专题"]){
        ZYLFastlookAllContentViewController *FastlookAllContentViewController = [[[ZYLFastlookAllContentViewController alloc] init] autorelease];
        FastlookAllContentViewController.ID = [model.topic objectForKey:@"id"];
        [self.navigationController pushViewController:FastlookAllContentViewController animated:YES];
        
    } else if ([keyword isEqualToString:@"评论"]){
        
        
    } else if ([keyword isEqualToString:@"分享"]){
        
        
    } else if ([keyword isEqualToString:@"作者"]){
        ZYLAuthorBaseViewController *authorBaseViewController = [[[ZYLAuthorBaseViewController alloc] init] autorelease];
        
        authorBaseViewController.authorId = [[model.topic objectForKey:@"user"] objectForKey:@"id"];
        
        [self.navigationController pushViewController:authorBaseViewController animated:YES];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/




@end
